import 'package:flutter/material.dart';
import 'dart:math' as Math;

enum CardSuit {
  Diamond,  //方块//小王
  Club,  //梅花//大王
  Heart,  //红桃
  Spade //黑桃
}

class PlayingCard {
  String num;
  CardSuit suit;
  bool isCoverd = false;
  bool isWait = false;  //待出
  static final double cardWidth = 56;
  static final double cardHeight = 86;
  static final double cardWidth2 = 29;
  static final double cardHeight2 = 42;
  static final double cardWidth3 = 22;
  static final double cardHeight3 = 32;
  static const double FONT_SIZE = 13;
  static const FontWeight FONT_WEIGHT = FontWeight.w700;
  double _cardWidth = cardWidth;
  double _cardHeight = cardHeight;
  FontWeight _fontWeight = FONT_WEIGHT;
  double _fontSize = FONT_SIZE;
  int sizeType = 1;
  bool isLandLord=false;
  static final Map cardSuitColor = {CardSuit.Spade: Colors.black, CardSuit.Heart: Colors.red, CardSuit.Club: Colors.black, CardSuit.Diamond: Colors.red};
  
  double get landlordCardIconSize => _fontSize * 1.5;
  double get landlordCardIconBottom => _fontSize - 8;
  
  static const List KING_STR = ['J', 'O', 'K', 'E', 'R'];
  static const Map<String, int> cards = {'30':0,'31':1,'32':2,'33':3,'40':4,'41':5,'42':6,'43':7,'50':8,'51':9,'52':10,'53':11,
  '60':12,'61':13,'62':14,'63':15,'70':16,'71':17,'72':18,'73':19,'80':20,'81':21,'82':22,'83':23,'90':24,'91':25,'92':26,'93':27,'100':28,'101':29,'102':30,'103':31,
  'J0':32,'J1':33,'J2':34,'J3':35,'Q0':36,'Q1':37,'Q2':38,'Q3':39,'K0':40,'K1':41,'K2':42,'K3':43,'A0':44,'A1':45,'A2':46,'A3':47,'20':48,'21':49,'22':50,'23':51,'W0':52,'W1':53};

  PlayingCard({@required String cardText, this.isCoverd=false, this.isWait = false, this.sizeType = 1, this.isLandLord=false}) {
    suit = CardSuit.values[int.parse(cardText.substring(cardText.length-1))];
    num = cardText.substring(0, cardText.length-1);
    setSizeBySizeType();
  }
  // PlayingCard({@required this.num, @required this.suit, this.isCoverd=false, this.isWait = false});
  PlayingCard.coverd({this.sizeType = 1, this.isLandLord=false}){
    isCoverd = true;
    setSizeBySizeType();
  }
  void setSizeBySizeType() {
    if(this.sizeType == 2) {
      _cardWidth = cardWidth2;
      _cardHeight = cardHeight2;
      _fontSize = 7;
      _fontWeight = FontWeight.w600;
    }else if(this.sizeType == 3) {
      _cardWidth = cardWidth3;
      _cardHeight = cardHeight3;
      _fontSize = 5.8;
      _fontWeight = FontWeight.w500;
    }
  }
  String get cardText => this.num + this.suit.index.toString();
  void triggerCover() {
    isCoverd = !isCoverd;
  }
  void triggerWait() {
    isWait = !isWait;
  }
  String getNum() {
    return num;
  }
  int getSuit() {
    return suit.index;
  }

  //获取花色图标
  Widget getSuitIcon() {
    switch (suit) {
      case CardSuit.Spade:
        return Icon(IconData(0xe60b, fontFamily: 'iconfont'),color: PlayingCard.cardSuitColor[suit],size: _fontSize);
        break;
      case CardSuit.Heart:
        return Icon(IconData(0xe60a, fontFamily: 'iconfont'),color: PlayingCard.cardSuitColor[suit],size: _fontSize);
        break;
      case CardSuit.Diamond:
        return Icon(IconData(0xe609, fontFamily: 'iconfont'),color: PlayingCard.cardSuitColor[suit],size: _fontSize);
        break;
      case CardSuit.Club:
        return Icon(IconData(0xe608, fontFamily: 'iconfont'),color: PlayingCard.cardSuitColor[suit],size: _fontSize);
        break;
      default: 
        return null;
        break;
    }
  }

  //普通牌图标+文字
  Widget getCommonTextureSide() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Text(num, style: TextStyle(fontSize: _fontSize, color: PlayingCard.cardSuitColor[suit], fontWeight: _fontWeight)),
        Container(
          margin: EdgeInsets.only(top: 0.2),
          child: getSuitIcon()
        )
      ]
    );
  }
  //普通牌
  Widget getCommonTexture() {
    return Row(
      children: <Widget>[
        Expanded(
          flex: 1,
          child: Container(
            alignment: Alignment.topLeft,
            child: getCommonTextureSide()
          ),
        ),
        Transform.rotate(
          angle: Math.pi,
          child: getCommonTextureSide(),
        )
      ]
    );
  }
  //王牌文字
  Widget getKingTextureSide() {
    Color kingColor = suit == CardSuit.Club ? Colors.red : Colors.black;
    List<Widget> textsWidget = PlayingCard.KING_STR.map((val)=>Text(val, style: TextStyle(color: kingColor, fontSize: _fontSize * 0.7, fontWeight: _fontWeight))).toList();

    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: textsWidget,
    );
  }
  //王牌
  Widget getKingTexture() {
    Color kingColor = suit == CardSuit.Club ? Colors.red : Colors.black;
    return Row(
      children: <Widget>[
        getKingTextureSide(),
        Expanded(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Icon(IconData(0xe6a8, fontFamily: 'iconfont'),color: kingColor, size: _fontSize*1.5),
              Icon(IconData(0xe6b9, fontFamily: 'iconfont'),color: kingColor, size: _fontSize*1.5),
            ]
          )
        ),
        Transform.rotate(
          angle: Math.pi,
          child: getKingTextureSide()
        )
      ],
    );
  }
  //创建牌面
  Widget getTexture() {
    if(isCoverd) {
      Widget icon = Icon(IconData(isLandLord ? 0xe603 : 0xe625, fontFamily: 'iconfont'), color: Colors.white, size: _cardWidth / 2);
      return Container(
        color: Color.fromARGB(255, 91, 160, 201),
        margin: EdgeInsets.all(1),
        child: isLandLord ? Transform.rotate(
          angle: -Math.pi / 5,
          child: icon
        ) : icon
      );
    }else {
      Widget texture = num.compareTo('W') != 0 ? getCommonTexture() : getKingTexture();
      return isLandLord ? Stack(
        children: <Widget>[
          texture,
          Positioned(bottom: landlordCardIconBottom, left: 0,
            child: Transform.rotate(
              angle: -Math.pi / 5,
              child: Icon(IconData(0xe603, fontFamily: 'iconfont'), color: Colors.blue, size: landlordCardIconSize)
            )
          )
        ],
      ) : texture;
    }
  }

  Widget getCardBody() {
    double panddingWidth = 3.5 - this.sizeType;
    double borderWidth = 0.3 * (4 - this.sizeType);
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(color: Color.fromARGB(200, 143, 143, 143), width: borderWidth),
        borderRadius: BorderRadius.circular(panddingWidth + 1)
      ),
      width: _cardWidth,
      height: _cardHeight,
      child: Container(
        padding: EdgeInsets.all(panddingWidth),
        child: getTexture()
      )
    );
  }
  Widget getCard() {
    return isWait ? Transform(
      transform: Matrix4.translationValues(0, -10, 0),
      child: getCardBody()
    ) : getCardBody();
  }
}